package com.example.nbshangpping.search;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.nbshangpping.R;
import com.example.nbshangpping.goods.DetailsGoodsActivity;
import com.example.nbshangpping.goods.GoodsBean;
import com.example.nbshangpping.goods.GoodsTypeAdapter;
import com.example.nbshangpping.utils.BaseActivity;
import com.example.nbshangpping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class QueryByNameActivity extends BaseActivity {
    ImageView backiv;
    EditText et;
    ImageButton btn;
    String name,hint;
    ListView lv;
    GoodsBean.DataBean dataBean;
    List<GoodsBean.DataBean> data;
    private GoodsTypeAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_by_name);
        initView();
    }

    private void initView() {
        backiv=findViewById(R.id.queryByName_iv_back);
        et=findViewById(R.id.queryByName_et);
        btn=findViewById(R.id.queryByName_btn);
        lv=findViewById(R.id.queryByName_lv);
        backiv.setOnClickListener(onClickListener);
        btn.setOnClickListener(onClickListener);
        hint=String.valueOf(getIntent().getCharSequenceExtra("hint"));
        name = String.valueOf(getIntent().getCharSequenceExtra("name"));
        et.setHint(hint);
        if(name=="null"){
            et.setText(hint);
        }else {
            et.setText(name);
        }
        lv.setOnItemClickListener(listener);
    }
    AdapterView.OnItemClickListener listener=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            GoodsBean.DataBean dataBean= data.get(position);
            Intent intent=new Intent(QueryByNameActivity.this, DetailsGoodsActivity.class);
            intent.putExtra("goods",dataBean);//将数据传递给下一级页面
            startActivity(intent);
        }
    };

    @Override
    protected void onStart() {
        super.onStart();
        Query();
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        GoodsBean goodsBean = new Gson().fromJson(result, GoodsBean.class);
        List<GoodsBean.DataBean> list = goodsBean.getData();
        data.addAll(list);
        if(data.size()==0){
            Toast.makeText(QueryByNameActivity.this,"未查询到相关商品",Toast.LENGTH_SHORT).show();
        }
        adapter.notifyDataSetChanged();
    }

    private void Query() {
        name= String.valueOf(et.getText());
        if(name.isEmpty()){
            name=hint;
        }
        et.setText(name);
        //数据源
        data = new ArrayList<>();
        //适配器
        adapter = new GoodsTypeAdapter(this, data);
        lv.setAdapter(adapter);
        //联网
        getNetwork(URLUtils.queryCommodityByName_url+name);
        //设置监听事件
    }

    View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.queryByName_iv_back:
                    finish();
                    break;
                case R.id.queryByName_btn:
                    Query();
                    break;
            }
        }
    };
}